/**
 * 初始化部门详情对话框
 */
var ProductInfoDlg = {
    productInfoData: {},
    zTreeInstance: null,
    editor: null,
    validateFields: {
        title: {
            validators: {
                notEmpty: {
                    message: '产品名称不能为空'
                }
            }
        },
        english_name: {
            validators: {
                notEmpty: {
                    message: '产品英文名不能为空'
                }
            }
        },
        pName: {
            validators: {
                notEmpty: {
                    message: '产品类型不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
ProductInfoDlg.clearData = function () {
    this.productInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ProductInfoDlg.set = function (key, val) {
    this.productInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ProductInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ProductInfoDlg.close = function () {
    parent.layer.close(window.parent.JhcbProduct.layerIndex);
}

/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
ProductInfoDlg.onClickProduct = function (e, treeId, treeNode) {
    $("#pName").attr("value", ProductInfoDlg.zTreeInstance.getSelectedVal());
    $("#product_category_id").attr("value", treeNode.id);
}

/**
 * 显示部门选择的树
 *
 * @returns
 */
ProductInfoDlg.showDeptSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentDeptMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};


/**
 * 隐藏部门选择的树
 */
ProductInfoDlg.hideDeptSelectTree = function () {
    $("#parentDeptMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

/**
 * 收集数据
 */
ProductInfoDlg.collectData = function () {
    this.productInfoData['description'] = ProductInfoDlg.editor.txt.html();
    this.set('id').set('title').set('english_name').set('enable').set('num').set('img_src').set('product_category_id');
};

/**
 * 验证数据是否为空
 */
ProductInfoDlg.validate = function () {
    $('#productInfoForm').data("bootstrapValidator").resetForm();
    $('#productInfoForm').bootstrapValidator('validate');
    return $("#productInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加产品
 */
ProductInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/portal/product/add", function (data) {
        Feng.success("添加成功!");
        window.parent.JhcbProduct.table.refresh();
        ProductInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.productInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
ProductInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/portal/product/update", function (data) {
        Feng.success("修改成功!");
        window.parent.JhcbProduct.table.refresh();
        ProductInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.productInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentDeptMenu" || $(
            event.target).parents("#parentDeptMenu").length > 0)) {
        ProductInfoDlg.hideDeptSelectTree();
    }
}

$(function () {
    Feng.initValidator("productInfoForm", ProductInfoDlg.validateFields);

    var ztree = new $ZTree("parentDeptMenuTree", "/portal/product/category/tree");
    ztree.bindOnClick(ProductInfoDlg.onClickProduct);
    ztree.init();
    ProductInfoDlg.zTreeInstance = ztree;

    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    editor.customConfig.uploadImgShowBase64 = true;
    editor.customConfig.uploadImgServer = '/portal/product/upload';
    editor.customConfig.uploadImgHeaders = {
        'Accept': 'multipart/form-data'
    };
    editor.customConfig.uploadFileName = 'file';
    editor.customConfig.debug = true;
    editor.customConfig.showLinkImg = false;
    editor.customConfig.uploadImgMaxSize = 10 * 1024 * 1024;
    editor.customConfig.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            console.log(result);
            var url = result.data;
            insertImg(url);
        }
    };
    editor.create();
    editor.txt.html($("#contentVal").text());
    ProductInfoDlg.editor = editor;

    // 初始化图片上传
    var avatarUp = new $WebUpload("img_src");
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();
});
